/*
{*******************************************************************}
{                                                                   }
{       KS-GANTT DotNet Library                                     }
{       Gantt-Charting Control for Windows Forms                    }
{                                                                   }
{       Copyright (c) 2009 - 2012 by Kroll-Software                 }
{       All Rights Reserved                                         }
{                                                                   }
{   This code is provided 'as is',                                  }
{   without warranties of any kind. Use at your own risk.           }                                                              
{                                                                   }
{   As a license holder of the KS-Gantt Control,                    }
{   you may freely redistribute executables based on                }
{   and created with these contents.                                }
{   You can modify the source code to meet your needs.              }
{                                                                   }
{   However, you must not publish, redistribute or transfer         }
{   this source code to 3rd parties under any conditions.           }
{                                                                   } 
{*******************************************************************}
*/

using System.Diagnostics;
using System;
using System.Data;
using System.Collections;
using System.Runtime.InteropServices;

namespace KS.Common
{
	public sealed class DatabaseHelpers
	{		
		private static string m_WindowsUser = "";
		
		public static string WindowsUser()
		{
            if (m_WindowsUser != "")
			{
                return m_WindowsUser;
			}
			else
			{
				string strUser = "";
				string[] varaUser;

				try
				{
					strUser = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
					varaUser = strUser.Split('\\');
                    m_WindowsUser = varaUser[1];
				}
				catch
				{
                    m_WindowsUser = KS.Common.Strings.GetSafeString(strUser);
				}

                return m_WindowsUser;
			}
		}
		
		public static string GetSQLServerConnectionString(string Server, string Database, bool bWinSecurity, string UserID, string Password, long ConnectionTimeout)
		{
			string returnValue;
			if (bWinSecurity)
			{
				returnValue = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=" + Database + ";Data Source=" + Server + ";Connect Timeout=" + ConnectionTimeout.ToString();
			}
			else
			{
				returnValue = "Password=" + Password + ";Persist Security Info=True;User ID=" + UserID + ";Initial Catalog=" + Database + ";Data Source=" + Server + ";Connect Timeout=" + ConnectionTimeout.ToString();
			}
			return returnValue;
		}

		public static string GetAccessConnectionString(string Filename, long ConnectionTimeout)
		{
			const string MDAC_PROVIDER = "Microsoft.Jet.OLEDB.4.0";            
			return "PROVIDER=" + MDAC_PROVIDER + ";Data Source=" + Filename + ";Persist Security Info=False";
		}
	}
	
}
